package ModelLayer; //makes this class a part of the ModelLayer package
import java.util.*;

/**
 * 
 * <code>DVD</code> class stores information about DVD and its copies.
 * It manages the data flow between <code>Copy</code> class and the DVD Controller.
 * 
 * @author (dmaj0913 - group 2) 
 * @version (2013.11.08)
 */
public class DVD
{
    private long id;
    private String title;
    private String artist;
    private String publicationDate;
    private ArrayList<Copy> listOfCopies;
    
    /**
     * Constructor creates new instance of <code>DVD</code> class.
     * @param id stores a number of type long that is the id of a DVD.
     * @param title takes a Character String with the title of a DVD.
     * @param title takes a Character String with the artist of a DVD.
     * @param title takes a Character String with the date of a DVD is added.
     */
    public DVD(long id, String title, String artist, String date)
    {
        this.id = id;
        this.title = title;
        this.artist = artist;
        this.publicationDate = date;
        listOfCopies = new ArrayList<Copy>();
    }
    /**
     * Returns the ID of a DVD.
     * @return a number of type long that is the ID of a DVD.
     */
    public long getId()
    {
        return id;
    }
    /**
     * Returns the Title of a DVD.
     * @return a Character String with the title of a DVD.
     */
    public String getTitle()
    {
        return title;
    }
    
    /**
     * Returns the Artist of a DVD.
     * @return a Character String with the artist of a DVD.
     */
    public String getArtist()
    {
        return artist;
    }
    /**
     * Returns the date of a DVD is added.
     * @return a Character String with the date of a DVD.
     */
    public String getDate()
    {
        return publicationDate;
    }
    /**
     * Changes the id of a DVD.
     * @param newvar stores the desired new id of a DVD.
     */
    public void setId(long newvar)
    {
        id = newvar;
    }
    
    /**
     * Changes the title of a DVD.
     * @param newvar stores the desired new title of a DVD.
     */
    public void setTitle(String newvar)
    {
        title = newvar;
    }
    
    /**
     * Changes the artist of a DVD.
     * @param newvar stores the desired new artist of a DVD.
     */
    public void setArtist(String newvar)
    {
        artist = newvar;
    }
    
    /**
     * Changes the date of a DVD.
     * @param newvar stores the desired new date of a DVD.
     */
    public void setDate(String newvar)
    {
        publicationDate = newvar;
    }
    /**
     * Adds a peson a unique copy to the collection of copies.
     * @param takes an object of type Copy that has to be added to the collection.
     * @return <code>false</code> if there is already a copy with that serail number and <true> if the copy was successfully added.
     */
    public boolean addCopy(Copy copy)
    {
        Iterator<Copy> it = listOfCopies.iterator();
        while(it.hasNext())
        {
            Copy test = it.next();
            if(copy.getSerialNumber() == test.getSerialNumber()) return true;
        }
        listOfCopies.add(copy);
        return false;
    }
       /**
     * Deletes a specific copy by finding it in the collection.
     * @param todelete stores a copy of type Copy that has to be deleted from the ArrayList of copies.
     */
    public void removeCopy(Copy todelete)
    {
        Iterator<Copy> it = listOfCopies.iterator();
        while(it.hasNext())
        {
            Copy testing = it.next();
            if(todelete.getSerialNumber() == testing.getSerialNumber()) it.remove();
        }
    }
        /**
     * Finds a copy in the collection and edits all its date and cost via the Terminal.
     * @param toupdate stores a copy of type Copy that has to be updated.
     * @param date stores a character string of the new date of the copy.
     * @param cost stores a number of type int that has to be the cost of the copy.
     */
    public void updateCopy(Copy toupdate, String date, int cost)
    {
        Iterator<Copy> it = listOfCopies.iterator();
        while(it.hasNext())
        {
            Copy testing = it.next();
            if(toupdate.getSerialNumber() == testing.getSerialNumber())
            {
                testing.setPurchaseDate(date);
                testing.setPurchaseCost(cost);
            }
        }
    }
    
    /**
     * Finds a copy by a specific Serial Number and returns it.
     * @param id stores the desired serial number of the copy that the system has to searching for.
     * @return The desired copy or <code>null</code> if it's not found in the System.
     */ 
    public Copy getCopy(long id)
    {
        Iterator<Copy> it = listOfCopies.iterator();
        while(it.hasNext())
        {
            Copy testing = it.next();
            if(testing.getSerialNumber() == id) return testing;
        }
        return null;
    }
    
    /**
     * Returns the collection of all copies.
     * @return an ArrayList of type Copy that contains all the copies of a DVD.
     */ 
    public ArrayList<Copy> getCopyList()
    {
        return listOfCopies;
    }
}
